#include "database.hh"

#include <cstdlib>

Database::Database (std::string dbname)
{
  std::ifstream is;

  std::cout << "Opening player database ..." << std::endl;
  is.open (dbname.c_str ());

  if (!is.is_open ())
  {
    std::cerr << "ERROR COULD NOT OPEN DATABASE FILE" << std::endl;
    exit (1);
  }

  std::string login, pass, rank;
  while (!is.eof ())
  {
    is >> login;
    is >> pass;
    is >> rank;
    players_[login] = new PlayerInfo (login, pass, rank);
  }

  std::cout << "DONE" << std::endl;

  is.close ();
}

Database::~Database ()
{
  players_.clear ();
}

PlayerInfo*
Database::find_player (std::string name)
{
  if (players_.find (name) == players_.end ())
    return 0;

  return players_[name];
}
